<template>
	<view>
    <my_search @click="gotoSearch"></my_search>
<view class="scroll-view-container">
  <!-- 左侧滑动 -->
  <scroll-view scroll-y="true" :style="{height:wh+'px'}" class="lest-scroll-view">
    <block v-for="(item,index) in cateList" :key="index">
      <view :class="['lest-scroll-view-item',index ===active ? 'active':' ']" @click="activeChanged(index)">{{item.cat_name}}</view>
    </block>
  </scroll-view>
  <!-- 右侧滑动 -->
  <scroll-view scroll-y="true" :style="{height:wh + 'px'}">
    <view class="cate-lv2" v-for="(item,index) in cateLevel2" :key="index">
      <!-- 二级分类的标题 -->
      <view class="cate-lv2-title">
        /{{item.cat_name}}/
      </view>
      <!-- 三级分类 -->
      <view class="cate-lv3-list">
        <view class="cate-lv3-item" v-for="(item,index) in item.children" :key="index" >
          <image :src="item.cat_icon" mode=""></image>
          <text>{{item.cat_name}}</text>
        </view>
      </view>
      
    </view>
  </scroll-view>
</view>
	</view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins:[badgeMix],
		data() {
			return {
				// 当前设备可用高度
        wh:0,
        // 第一项被激活
        active:0,
        // 分类列表数据
        cateList:[],
        // 二级分类
        cateLevel2:[]
			};
		},
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight
      this.getCateList()
    },
    methods:{
      async getCateList(){
        const {data:res} = await uni.$http.get('/api/public/v1/categories')
        if(res.meta.status !==200) return uni.$showMsg()
        this.cateList = res.message
        
        // 怼二级分类负值
        this.cateLevel2 = res.message[0].children
      },
      activeChanged(index){
        this.active = index
        
        // 重新为二级分类赋值
        this.cateLevel2 = this.cateLevel2[index].children
      },
      gotoSearch(){
        uni.navigateTo({
          url:'../../subpkg/search/search'
        })
      }
    }
	}
</script>

<style lang="scss">
.scroll-view-container{
  display: flex;
  .lest-scroll-view{
    width:120px;
    .lest-scroll-view-item{
      background-color: #F7F7F7;
      line-height: 60px;
      text-align: center;
      font-size: 12px;
      
      &.active{
        background-color: #FFFFFF;
        // 相对定位
        position: relative;
        
        // 给active添加一个为元素
        &::before{
          content: (' ');
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          // 绝对路径
          position: absolute;
          top: 50%;
          left: 0;
          // 向上撤
          transform: translateY(-50%);
        }
      }
    }
  }
}
.cate-lv2-title {
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  padding: 15px 0;
}
.cate-lv3-list {
  display: flex;
  flex-wrap: wrap;

  .cate-lv3-item {
    width: 33.33%;
    margin-bottom: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;

    image {
      width: 60px;
      height: 60px;
    }

    text {
      font-size: 12px;
    }
  }
}
</style>
